package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.CourseOrder;
import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.mapper.CourseOrderMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ICourseOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CourseOrderServiceImpl implements ICourseOrderService {

    @Autowired
    private CourseOrderMapper courseOrderMapper;

    @Override
    public void delete(Long id) {
        courseOrderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(CourseOrder courseOrder) {
        // 1.自动设置销售时间为当前时间
        courseOrder.setInputTime(new Date());
        // 2.原本的潜在客户的状态需要修改为正式客户状态
        if(courseOrder.getCustomer().getStatus() == 0){
            courseOrder.getCustomer().setStatus(Customer.STATUS_NORMAL);
        }
        courseOrderMapper.insert(courseOrder);
    }

    @Override
    public CourseOrder get(Long id) {
        return courseOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CourseOrder> listAll() {
        return courseOrderMapper.selectAll();
    }

    @Override
    public void update(CourseOrder courseOrder) {

        // 1.自动设置销售时间为当前时间
        courseOrder.setInputTime(new Date());
        courseOrderMapper.updateByPrimaryKey(courseOrder);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        /*int count = courseOrderMapper.selectForCount(qo);
        // 判断是否有数据
        if(count == 0){
            return new PageResult(qo.getPageSize());
        }*/
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());  // 返回页码和每页数量

        // 查询结果集
        List<CourseOrder> list = courseOrderMapper.selectForList(qo);
//        return new PageResult(qo.getCurrentPage(),count,list,qo.getPageSize());
        return new PageInfo(list);
    }
}
